<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class mis_jenis_barang extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		$this->load->model('mis_jenis_barang_model', 'JenisBarang');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Jenis Barang";
		viewByTemplate('mis_jenis_barang/index', $data, 'grid');
	}

	function id_auto(){
		$query = $this->db->query("SELECT id_barang FROM (SELECT * FROM mis_jenis_barang ORDER BY id_barang DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_barang'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_barang_auto'] = $autonum;

		$query = $this->db->query("SELECT kode_jenis_barang FROM (SELECT * FROM mis_jenis_barang ORDER BY kode_jenis_barang DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_jenis_barang'];
		else
			$MaxID = "KB0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_jenis_barang_auto'] = "KB" . sprintf("%07s",$NextID);
		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->JenisBarang->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		
		if (post("kode_jenis_barang") == '') {
			$error->Add("kode_jenis_barang", "Kode Jenis Barang", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM mis_jenis_barang WHERE kode_jenis_barang = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kd_jenis_barang"))))."'")) > 0) {
			$error->Add("kode_jenis_barang", "Kode Jenis Barang", $tpl_duplicate);
		} else if (post("nama_jenis_barang") == '') {
			$error->Add("nama_jenis_barang", "Nama Jenis Barang", $tpl_empty);
		}

		if (post("nama_jenis_barang") == '') {
			$error->Add("nama_jenis_barang", "Nama Jenis Barang", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		//$ls_result		= "<script>loadPage(gs_path + '/' + controller + '/index');</script>";
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kode_jenis_barang', 'nama_jenis_barang','id_barang'));
				$data['kode_jenis_barang']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kode_jenis_barang'])));
				
				if (post("mode") == "create")
				{
					
					$this->JenisBarang->Add($data);

					$ls_response = "Data Tersimpan";
					
				}
				else if (post("mode") == "edit")
				{
					
					$this->JenisBarang->Update($data, 
										array('id_barang'	=> $data['id_barang']));

					$ls_response = "Data Tersimpan [edited]";
					

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}

			
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));

		//echo $ls_result;
	}

	function ajax_delete()
		{
			$ls_result		= "";
			$pesan 			= "Data Terhapus";
			$id_barang 	= get('id_barang');
			
			if ($id_barang != "")
			{
				// Delete Single
				$arr_rule	= array('id_barang' => $id_barang);
				$ls_result = $this->JenisBarang->DeleteSingle($arr_rule);

			}
			else
			{
				
				$num_row = intval($this->data['num_row']['deleted']);
				
				for ($i = 0; $i < $num_row; $i++)
				{
					$row	= $this->data['deleted'][$i];
					
					$arr_rule	= array('id_barang' => $row['id_barang']);
					$ls_result .= $this->JenisBarang->DeleteSingle($arr_rule);
				}
			}
			
			echo $pesan;
			echo "
				<script>
					loadPage(gs_path + '/' + controller + '/index');
				</script>
			";	
	}
}
?>